﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using VisualWip.Core;

namespace VisualWip.CoreTests
{
    [TestClass]
    public class PersistenceTreeTest
    {
        public TestContext TestContext { get; set; }

        [TestMethod]
        public void SimpleSave()
        {
            string name = "MySetting";
            string nameOne = "One";
            string childName = "Child";

            var myClass = new MySimpleClass {NameOne = nameOne};
            myClass.List.Add(new MySimpleChildClass {ChildName = childName});
            PersistenceTree sut = myClass.Persist(name);

            Assert.AreEqual(name, sut.Name);
            Assert.AreEqual(nameOne, sut.Get(() => myClass.NameOne,string.Empty));
        }

        [TestMethod]
        public void SimpleLoad()
        {
            string name = "MySetting";
            string nameOne = "One";
            string childName = "Child";

            var myClass = new MySimpleClass {NameOne = nameOne};
            myClass.List.Add(new MySimpleChildClass {ChildName = childName});
            PersistenceTree sut = myClass.Persist(name);

            var result = sut.Create<MySimpleClass>();

            Assert.AreEqual(nameOne, result.NameOne);
            Assert.AreEqual(childName, result.List[0].ChildName);
        }

        [TestMethod]
        public void SimpleDerivedLoad()
        {
            string name = "MySetting";
            string nameOne = "One";
            string childName = "Child";

            var myClass = new MySimpleClass {NameOne = nameOne};
            myClass.List.Add(new MySimpleChildClassDerived {ChildName = childName});
            PersistenceTree sut = myClass.Persist(name);

            var result = sut.Create<MySimpleClass>();

            Assert.IsInstanceOfType(result.List[0], typeof (MySimpleChildClassDerived));
        }

        [TestMethod]
        public void SimpleCreateFromStrings()
        {
            string name = "MySetting";
            string typeName = typeof (MySimpleClass).AssemblyQualifiedName;
            string childTypeName = typeof (MySimpleChildClass).AssemblyQualifiedName;
            string nameOne = "One";

            var sut = new PersistenceTree(name, typeName);
            sut.Add("NameOne", nameOne);
            sut.AddChild(new PersistenceTree("List", childTypeName));

            Assert.AreEqual(name, sut.Name);
            Assert.AreEqual(nameOne, sut.Get<string>("NameOne",string.Empty));
        }
    }
}